package com.chinaums.xm.wechat.service.wechat;

import java.util.List;
import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chinaums.xm.wechat.WeixinConstant;
import com.chinaums.xm.wechat.domain.wechat.AccessToken;
import com.chinaums.xm.wechat.domain.wechat.user.OpenIdList;
import com.chinaums.xm.wechat.domain.wechat.user.Tag;
import com.chinaums.xm.wechat.domain.wechat.user.TagList;
import com.chinaums.xm.wechat.domain.wechat.user.Tags;
import com.chinaums.xm.wechat.domain.wechat.user.UserInfo;
import com.chinaums.xm.wechat.domain.wechat.user.UserList;
import com.chinaums.xm.wechat.service.Communicator;

import net.sf.json.JSONObject;

/**
 * 用户管理service
 * 包含
 * 1、用户标签管理
 * 2、设置用户备注名		
 * @author chenjf
 *
 */
@Service
public class UserService {
	
	@Resource
	private Communicator communicator;
	
	@Resource
	private CommonService commonService;
	
	/**
	 * 更新用户备注名
	 * @param openid
	 * @param remark
	 * @return
	 */
	public Optional<UserInfo> getUserInfo(final String openid){
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String url = WeixinConstant.USER_INFO_QUERY_URL.replace("ACCESS_TOKEN", accessToken.get().getToken())
					.replace("OPENID", openid);
			final Optional<String> response = communicator.getRequest(url);
			if(response.isPresent()){
				final JSONObject json = JSONObject.fromObject(response.get());
				final UserInfo userInfo = (UserInfo)JSONObject.toBean(json, UserInfo.class);
				return Optional.of(userInfo);
			}
		}
		return Optional.empty();
	}
	
	/**
	 * 更新用户备注名
	 * @param openid
	 * @param remark
	 * @return
	 */
	public boolean updateUserRemark(final String openid,final String remark){
		boolean result = false;
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String url = WeixinConstant.USER_UPDATEREMARK_URL.replace("ACCESS_TOKEN", accessToken.get().getToken());
			final UserInfo info = new UserInfo();
			info.setOpenid(openid);
			info.setRemark(remark);
			final JSONObject request = JSONObject.fromObject(info);
			final Optional<String> responseOpt = communicator.postRequest(url, String.valueOf(request));
			final String msg = responseOpt.get();
			final JSONObject response = JSONObject.fromObject(msg);
			final String errcode = response.getString("errcode");
			if(errcode.equals("0")){
				result = true;
			}
		}
		return result;
	}
	
	/**
	 * 查询用户列表
	 * @return
	 */
	public Optional<UserList> queryUser(){
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String url = WeixinConstant.USER_LIST_QUERY_URL.replace("ACCESS_TOKEN", accessToken.get().getToken());
			final Optional<String> response = communicator.getRequest(url);
			if(response.isPresent()){
				final JSONObject json = JSONObject.fromObject(response.get());
				final UserList responseList = (UserList) JSONObject.toBean(json, UserList.class);
				return Optional.of(responseList);
			}
		}
		return Optional.empty();
	}
	
	/**
	 * 根据标签名称创建标签
	 * @param tagName
	 * @return
	 */
	public Optional<Tags> createTag(final String tagName){
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String url = WeixinConstant.TAG_CREATE_URL.replace("ACCESS_TOKEN", accessToken.get().getToken());
			final Tags tags = new Tags();
			final Tag tag = new Tag();
			tag.setName(tagName);
			tags.setTag(tag);
			final String json = JSONObject.fromObject(tags).toString();
			final Optional<String> response = communicator.postRequest(url, json);
			if(response.isPresent()){
				final String msg = response.get();
				final Tags responseTag = (Tags) JSONObject.toBean(JSONObject.fromObject(msg), Tags.class);
				return Optional.of(responseTag);
			}
		}
		return Optional.empty();
	}
	
	/**
	 * 查询所有标签
	 * @return
	 */
	public Optional<TagList> queryTag(){
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String url = WeixinConstant.TAG_QUERY_URL.replace("ACCESS_TOKEN", accessToken.get().getToken());
			final Optional<String> response = communicator.getRequest(url);
			if(response.isPresent()){
				final String msg = response.get();
				final TagList responseTag = (TagList) JSONObject.toBean(JSONObject.fromObject(msg), TagList.class);
				return Optional.of(responseTag);
			}
		}
		return Optional.empty();
	}
	
	/**
	 * 更新标签
	 * @param id
	 * @param tagName
	 * @return
	 */
	public boolean updateTag(final String id,final String tagName){
		boolean result = false;
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String url = WeixinConstant.TAG_UPDATE_URL.replace("ACCESS_TOKEN", accessToken.get().getToken());
			final Tags tags = new Tags();
			final Tag tag = new Tag();
			tag.setId(id);
			tag.setName(tagName);
			tags.setTag(tag);
			final String json = JSONObject.fromObject(tags).toString();
			final Optional<String> responseOpt = communicator.postRequest(url, json);
			if(responseOpt.isPresent()){
				final String msg = responseOpt.get();
				final JSONObject response = JSONObject.fromObject(msg);
				final String errcode = response.getString("errcode");
				if(errcode.equals("0")){
					result = true;
				}
			}
		}
		return result;
	}
	
	/**
	 * 删除标签
	 * @param id
	 * @return
	 */
	public boolean deleteTag(final String id){
		boolean result = false;
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String url = WeixinConstant.TAG_DELETE_URL.replace("ACCESS_TOKEN", accessToken.get().getToken());
			final Tags tags = new Tags();
			final Tag tag = new Tag();
			tag.setId(id);
			tags.setTag(tag);
			final String json = JSONObject.fromObject(tags).toString();
			final Optional<String> responseOpt = communicator.postRequest(url, json);
			if(responseOpt.isPresent()){
				final String msg = responseOpt.get();
				final JSONObject response = JSONObject.fromObject(msg);
				final String errcode = response.getString("errcode");
				if(errcode.equals("0")){
					result = true;
				}
			}
		}
		return result;
	}
	
	/**
	 * 批量打标签
	 * @param id
	 * @return
	 */
	public boolean tagForUser(List<String> list){
		boolean result = false;
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String url = WeixinConstant.BATCH_TAG_URL.replace("ACCESS_TOKEN", accessToken.get().getToken());
			OpenIdList tags = new OpenIdList();
			tags.setOpenid_list(list);
			tags.setTagid("100");
			final String json = JSONObject.fromObject(tags).toString();
			final Optional<String> responseOpt = communicator.postRequest(url, json);
			if(responseOpt.isPresent()){
				final String msg = responseOpt.get();
				final JSONObject response = JSONObject.fromObject(msg);
				final String errcode = response.getString("errcode");
				if(errcode.equals("0")){
					result = true;
				}
			}
		}
		return result;
	}
	/**
	 * 批量删除标签
	 * @param id
	 * @return
	 */
	public boolean batchDelTag(List<String> list){
		boolean result = false;
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String url = WeixinConstant.BATCH_DEL_TAG_URL.replace("ACCESS_TOKEN", accessToken.get().getToken());
			OpenIdList tags = new OpenIdList();
			tags.setOpenid_list(list);
			tags.setTagid("100");
			final String json = JSONObject.fromObject(tags).toString();
			final Optional<String> responseOpt = communicator.postRequest(url, json);
			if(responseOpt.isPresent()){
				final String msg = responseOpt.get();
				final JSONObject response = JSONObject.fromObject(msg);
				final String errcode = response.getString("errcode");
				if(errcode.equals("0")){
					result = true;
				}
			}
		}
		return result;
	}
}
